package com.flea.web.controller;

import com.flea.model.Order;
import com.flea.model.Result;
import com.flea.service.OrderService;
import com.flea.util.SessionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @return 订单详情
     */
    @ResponseBody
    @PostMapping("/orders")
    public Result<Order> create(HttpSession session, int productId, short quantity) {
        Integer buyerId = SessionUtils.getSessionUser(session).getId();
        try {
            Order order = orderService.buy(buyerId, productId, quantity);
            return Result.success(order);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

}
